package cn.lysy.xiaoruan.activity;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioGroup;

import cn.lysy.xiaoruan.MainActivity;
import cn.lysy.xiaoruan.R;
import cn.lysy.xiaoruan.base.BaseActivity;
import cn.lysy.xiaoruan.fragment.AllOrderFragment;
import cn.lysy.xiaoruan.fragment.CommentOrderFragment;
import cn.lysy.xiaoruan.fragment.GetOrderFragment;
import cn.lysy.xiaoruan.fragment.PayOrderFragment;

public class OrderActivity extends BaseActivity {

    private RadioGroup radioGroup;
    private ImageButton order_back;
    private AllOrderFragment allOrderFragment;
    private PayOrderFragment payOrderFragment;
    private GetOrderFragment getOrderFragment;
    private CommentOrderFragment commentOrderFragment;

    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        order_back= (ImageButton) findViewById(R.id.order_back);
        radioGroup= (RadioGroup) findViewById(R.id.order_radioG);
        fragmentManager=getSupportFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i){
                    case R.id.rt_order_all:
                        allOrderFragment=new AllOrderFragment();
                        replace(allOrderFragment);
                        break;
                    case R.id.rt_order_pay:
                        payOrderFragment=new PayOrderFragment();
                        replace(payOrderFragment);
                        break;
                    case R.id.rt_order_get:
                        getOrderFragment=new GetOrderFragment();
                        replace(getOrderFragment);
                        break;
                    case R.id.rt_order_comment:
                        commentOrderFragment=new CommentOrderFragment();
                        replace(commentOrderFragment);
                        break;
                }
            }
        });

        order_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        int orderid = getIntent().getIntExtra("orderid",0);

        switch( orderid ){
            case 1 : allOrderFragment = new AllOrderFragment();
                replace(allOrderFragment);
                radioGroup.check(R.id.rt_order_all);
                break;
            case 2 : payOrderFragment=new PayOrderFragment();
                replace(payOrderFragment);
                radioGroup.check(R.id.rt_order_pay);
                break;
            case 3 :  getOrderFragment=new GetOrderFragment();
                replace(getOrderFragment);
                radioGroup.check(R.id.rt_order_get);
                break;
            case 4 : commentOrderFragment=new CommentOrderFragment();
                replace(commentOrderFragment);
                radioGroup.check(R.id.rt_order_comment);
                break;
            default:
                break;
        }
        super.onResume();
    }

    private void replace(Fragment fragment) {
        fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.order_framelayout,fragment);
        fragmentTransaction.commit();
    }
}
